import { environment } from '@env/environment';
import { Injectable } from '@angular/core';
import { ApiService } from '@core/net/api.service';
import { IHttpRes } from '@core/net/HttpResponse';
import { IBaseArea, IBaseAddr, IBaseTag, IBaseShedule } from '@model/base';
import { HttpClient, HttpRequest, HttpResponse } from '@angular/common/http';
import { saveAs as importedSaveAs } from 'file-saver';

@Injectable({
	providedIn: 'root'
})
export class BaseService {

	constructor(
		private apiService: ApiService,
		private http: HttpClient
	) { }




	/**
	 * 二维码导出
	 * @param areaId 
	 * @returns 
	 */
	downloadQrCode(item, fileName: string) {
		const { areaLevel, id } = item;
		// const req = new HttpRequest('GET', `${environment.API_DOMAIN.default}/info/tdataareaaddr/qrcode/${areaLevel}/${id}`, null, {
		// 	responseType: 'blob'
		// });
		// this.http.request(req).subscribe((response: HttpResponse<Blob>) => {
			// if (response.headers) {
			// 	const contentDisp = response.headers.get('Content-Disposition');
			// 	let fileName = contentDisp.split(';')[1];
			// 	fileName = decodeURI(fileName.replace(/filename=/g, ''));
				// importedSaveAs(response, 'qrcode.zip');
			// }
		// })

		this.http.get(`${environment.API_DOMAIN.default}/info/tdataareaaddr/qrcode/${areaLevel}/${id}`,
			{
				responseType: 'blob'
			}).subscribe((response) => {
				// if (response.headers) {
				// 	const contentDisp = response.headers.get('Content-Disposition');
				// 	let fileName = contentDisp.split(';')[1];
				// 	fileName = decodeURI(fileName.replace(/fileName=/g, ''));
					importedSaveAs(response, fileName + '.zip');
				// }
				
			})

		// return `${environment.API_DOMAIN.default}/info/tdataareaaddr/qrcode/${areaLevel}/${id}`
	}

	/**
	 * 社区列表
	 * @param params 
	 * @returns 
	 */
	getAddrList(params): Promise<IHttpRes<Array<IBaseAddr>>> {
		return this.apiService.get('/info/tdataareaaddr/list', params).toPromise();
	}

	/**
	 * 删除社区
	 * @param id 
	 * @returns 
	 */
	deleteAddr(id: number): Promise<IHttpRes<any>> {
		return this.apiService.post('/info/tdataareaaddr/delete/' + id).toPromise();
	}


	/**
	 * 社区详情
	 * @param id 
	 * @returns 
	 */
	getAddrInfo(id: number): Promise<IHttpRes<any>> {
		return this.apiService.get('/info/tdataareaaddr/info/' + id).toPromise();
	}
	/**
	 * 新增社区
	 * @param body 
	 * @returns 
	 */
	saveAddr(body): Promise<IHttpRes<any>> {
		return this.apiService.post('/info/tdataareaaddr/save', body).toPromise();
	}

	/**
	 * 获取社区树
	 * @returns 
	 */
	getCommunityTree(): Promise<IHttpRes<Array<IBaseAddr>>> {
		return this.apiService.get('/info/tdataareaaddr/getCommunityTree').toPromise();
	}


	/**
	 * 村户标签列表
	 * @param params 
	 * @returns 
	 */
	getHouseholdTagList(params?): Promise<IHttpRes<Array<IBaseTag>>> {
		return this.apiService.post('/info/householdTag/list', params ? params : '').toPromise();
	}

	/**
	 * 删除村户标签
	 * @param id 
	 * @returns 
	 */
	deleteHouseholdTag(id: string): Promise<IHttpRes<any>> {
		return this.apiService.post('/info/householdTag/delete/' + id).toPromise();
	}


	/**
	 * 村户标签详情
	 * @param id 
	 * @returns 
	 */
	getHouseholdTagInfo(id: number): Promise<IHttpRes<any>> {
		return this.apiService.get('/info/householdTag/info/' + id).toPromise();
	}
	/**
	 * 新增村户标签
	 * @param body 
	 * @returns 
	 */
	saveHouseholdTag(body): Promise<IHttpRes<any>> {
		return this.apiService.post('/info/householdTag/save', body).toPromise();
	}








	/**
	 * 网格区列表
	 * @param params 
	 * @returns 
	 */
	getGridAreaList(params?): Promise<IHttpRes<{ list: Array<IBaseArea>, totalCount: number }>> {
		return this.apiService.post('/info/tdatagridarea/list', null, {
			params: params ? params : ''
		}).toPromise();
	}

	/**
	 * 删除网格区
	 * @param id 
	 * @returns 
	 */
	deleteGridArea(id: string): Promise<IHttpRes<any>> {
		return this.apiService.post('/info/tdatagridarea/delete/' + id).toPromise();
	}


	/**
	 * 网格区详情
	 * @param id 
	 * @returns 
	 */
	getGridAreaInfo(id: string): Promise<IHttpRes<any>> {
		return this.apiService.get('/info/tdatagridarea/info/' + id).toPromise();
	}
	/**
	 * 新增网格区
	 * @param body 
	 * @returns 
	 */
	saveGridArea(body): Promise<IHttpRes<any>> {
		return this.apiService.post('/info/tdatagridarea/save', body).toPromise();
	}






	/**
	 * 定时任务列表
	 * @param params 
	 * @returns 
	 */
	getScheduleList(params?): Promise<IHttpRes<{ list: Array<IBaseShedule>, totalCount: number }>> {
		return this.apiService.post('/sys/schedule/list', params).toPromise();
	}

	/**
	 * 删除定时任务
	 * @param id 
	 * @returns 
	 */
	deleteSchedule(id: number): Promise<IHttpRes<any>> {
		return this.apiService.post('/sys/schedule/delete/' + id).toPromise();
	}


	/**
	 * 定时任务详情
	 * @param id 
	 * @returns 
	 */
	getScheduleInfo(id: number): Promise<IHttpRes<any>> {
		return this.apiService.get('/sys/schedule/info/' + id).toPromise();
	}
	/**
	 * 新增定时任务
	 * @param body 
	 * @returns 
	 */
	saveSchedule(body): Promise<IHttpRes<any>> {
		return this.apiService.post('/sys/schedule/save', body).toPromise();
	}
	/**
	 * 暂停定时任务
	 * @param id 
	 * @returns 
	 */
	pauseSchedule(id: number): Promise<IHttpRes<any>> {
		return this.apiService.post(`/sys/schedule/pause/${id}`).toPromise();
	}


	/**
	 * 恢复定时任务
	 * @param id 
	 * @returns 
	 */
	resumeSchedule(id: number): Promise<IHttpRes<any>> {
		return this.apiService.post(`/sys/schedule/resume/${id}`).toPromise();
	}

	/**
	 * 立即执行任务
	 * @param id 
	 * @returns 
	 */
	runSchedule(id: number): Promise<IHttpRes<any>> {
		return this.apiService.post(`/sys/schedule/run/${id}`).toPromise();
	}




	// 	POST
	// /info/delete/{id}
	// 消息删除
	/**
	 * 消息删除
	 * @returns 
	 */
	deleteNotice(id: number) {
		return this.apiService.post(`/info/delete/${id}`).toPromise();
	}

	/**
	 * 获取消息列表
	 * @returns 
	 */
	getNoticeList(body) {
		return this.apiService.post('/info/list', null, {
			params: body
		}).toPromise();
	}


	/**
	 * 消息已读
	 * @returns 
	 */
	updateNotice(id: number) {
		return this.apiService.post(`/info/update/${id}`).toPromise();
	}

	/**
	 * 获取消息数
	 * @returns 
	 */
	getNoticeNum() {
		return this.apiService.post(`/info/getNoticeNum`).toPromise();
	}
}
